<?php
/*
 * 应用程序创建类
 */
class CApplication {
	public function __construct(){
		$param = new CParam();
		define('ROUTE_M',$param->route_m());
		define('ROUTE_C',$param->route_c());
		define('ROUTE_A',$param->route_a());
		$this->init(); 
	}
	
	public function init(){
		$controller = $this->load_controller();
		if(method_exists($controller,ROUTE_A)){
			if(preg_match('/^[_]/i',ROUTE_A)){
				exit('You are visiting the action is to protect the private action');
			} else {					
				call_user_func(array($controller,ROUTE_A));
			}
		}else {
			exit('Action does not exist.');
		}
	}
	
	public function load_controller($filename = '', $m = ''){
		if(empty($filename)) $filename = ROUTE_C;
		if(empty($m)) $m = ROUTE_M;
		$path = RF_PATH.'modules'.DIRECTORY_SEPARATOR.$m.DIRECTORY_SEPARATOR.$filename.'Controller.php';
		if(file_exists($path)){
			$classname = $filename.'Controller';
			include $path;			
			return new $classname;
		} else {
			exit('Controller does not exist.');
		}
	}
}
